package com.abc.cloud.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.abc.cloud.dto.perms.PermsCategory;
import com.abc.cloud.dto.perms.PermsTree;
import com.abc.cloud.vo.perms.PermsSave;
import com.abc.cloud.vo.perms.PermsUpdate;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.abc.cloud.common.utils.PageUtil;
import com.abc.cloud.common.utils.Query;

import com.abc.cloud.dao.PermsDao;
import com.abc.cloud.entity.PermsEntity;
import com.abc.cloud.service.PermsService;


@Service("permsService")
public class PermsServiceImpl extends ServiceImpl<PermsDao, PermsEntity> implements PermsService {

    @Override
    public PageUtil queryPage(Map<String, Object> params) {
        IPage<PermsEntity> page = this.page(
                new Query<PermsEntity>().getPage(params),
                new QueryWrapper<PermsEntity>()
        );

        return new PageUtil(page);
    }

    @Override
    public List<String> queryUserPermsList(Long userId) {
        return this.baseMapper.queryUserPermsList(userId);
    }

    @Override
    public List<PermsEntity> queryPermsList(){
        QueryWrapper<PermsEntity> permsEntityQueryWrapper = new QueryWrapper<>();
        permsEntityQueryWrapper.eq("status", 1);
        return this.baseMapper.selectList(permsEntityQueryWrapper);
    }

    @Override
    public PermsEntity getPermsInfoById(Long permsId) {
        QueryWrapper<PermsEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.eq("id", permsId);
        return this.baseMapper.selectOne(queryWrapper);
    }

    @Override
    public PermsEntity savePermsInfo(PermsSave permsSave) {
        PermsEntity tbPerms = BeanUtil.copyProperties(permsSave, PermsEntity.class);
        int insert = 0;
        // 判断是保存权限分类还是权限
        if(permsSave.getPermsCategoryId() == 0) {
            System.out.println(tbPerms);
            insert = this.baseMapper.savePermsCategory(tbPerms);
        }
        else {
            insert = this.baseMapper.insert(tbPerms);
        }
        if(insert <= 0){
            return null;
        }
        return tbPerms;
    }

    @Override
    public PermsEntity updatePermsInfo(PermsUpdate permsUpdate) {
        PermsEntity tbPerms = this.getPermsInfoById(permsUpdate.getId());
        BeanUtils.copyProperties(permsUpdate, tbPerms);
        int i = this.baseMapper.updateById(tbPerms);
        if(i <= 0){
            return null;
        }
        return this.baseMapper.selectById(tbPerms.getId());
    }

    @Override
    public Boolean deletePerms(Long permsId) {
        PermsEntity tbPerms = this.getPermsInfoById(permsId);
        tbPerms.setStatus(2);
        int i = this.baseMapper.updateById(tbPerms);
        if(i <= 0){
            return false;
        }
        return true;
    }

    @Override
    public List<PermsTree> getPermsList() {
        // 查询所有菜单分类
        List<PermsCategory> tbPermsCategories = this.baseMapper.selectPermsCategoryList();
        // 查询所有权限菜单
        QueryWrapper<PermsEntity> queryWrapper = new QueryWrapper<>();

        List<PermsEntity> tbPermList = this.baseMapper.selectList(queryWrapper);
        // 结果
        List<PermsTree> trees = new ArrayList<>();
        for (PermsCategory tbPermsCategory: tbPermsCategories){
            PermsTree permsTree = new PermsTree();
            List<PermsEntity> permsTrees = new ArrayList<>();
            for (PermsEntity tbPerms: tbPermList){
                if (tbPerms.getPermsCategoryId().equals(tbPermsCategory.getId())){
                    permsTrees.add(tbPerms);
                }
            }
            permsTree.setName(tbPermsCategory.getPermsName());
            permsTree.setId(tbPermsCategory.getId());
            permsTree.setStatus(tbPermsCategory.getStatus());
            permsTree.setCreateTime(tbPermsCategory.getCreateTime());
            permsTree.setPermsTrees(permsTrees);
            trees.add(permsTree);
        }
        return trees;
    }

    @Override
    public List<PermsCategory> getPermsCategory() {
        return this.baseMapper.selectPermsCategoryList();
    }
}